<html lang="en">
<head>
<meta charset="utf-8">
<title>版本管理</title>
<link href="/static/css/start/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<link href="/static/css/box.css" rel="stylesheet">
<link href="/static/css/table.css" rel="stylesheet">
<link href="/static/css/pages.css" rel="stylesheet">
<script src="/static/js/jquery-1.10.2.js"></script>
<script src="/static/js/jquery-ui-1.10.4.custom.js"></script>
<script src="/static/js/helper.js"></script>
<script src="/static/js/pages.js"></script>
<script>
var versionOnEdit = false;
var versionsData = [];
function clearText() {
    $('#addversionName').val('');
    $('#addversionDescription').val('');
    $('#versionID').val('');
    $("#branchName").val('')

    $("#subBranchName").val('')
    $("#addversionFullName").val('')
    $("#baseversionFullName").val('')

    $('#addversionSendMail').attr("checked", true);
    $('#addversionSendMail').prop("checked", true);
}
function versionChangeToEdit(edit, index) {
    versionOnEdit = edit;
    if (edit) {
        $('#editversionOK').show();
        $('#editversionCancel').show();
        $('#addversion').hide();
        $('#list').hide();
        var id = versionsData[index].id;
        var branch = versionsData[index].branch;
        var subBranch = versionsData[index].subBranch;
        var name = versionsData[index].name;
        var fullName = versionsData[index].fullName;
        var parentFullName = versionsData[index].parentFullName;
        var desc = versionsData[index].desc;
        $('#branchName').val(branch);
        $('#subBranchName').val(subBranch);
        $('#addversionName').val(name);
        $('#addversionFullName').val(fullName);
        $('#baseVersionFullName').val(parentFullName);
        $('#addversionDescription').val(desc);
        $('#versionID').val(id);
        $('#addversionSendMail').attr("checked", false);
        $('#addversionSendMail').prop("checked", false);
    }
    else {
        $('#editversionOK').hide();
        $('#editversionCancel').hide();
        $('#addversion').show();
        $('#list').show();
        clearText();
    }
}
function delversion(id) {
    callback = function () {
        $.post('delVersion.py', {id: id},
                function (result, status) {
                    if (status == 'success') {
                        if (result.success) {
                            showMessage('删除成功');
                            loadVersions();
                        } else {
                            showMessage(result.message);
                        }
                    }
                });
    };
    deleteConfim(callback);
}
function loadVersions() {
    cache = {};
    $.post('getVersions.py', {p: pageIndex, keyword: $('#versionNameKey').val()},
            function (result, status) {
                if (status == 'success') {
                    if (result.success) {
                        html = [];
                        versionsData = result.result;
                        pageIndex = result.pageIndex;
                        pageCount = result.pageCount;
                        for (i in result.result) {
                            html.push('<tr onmousemove="highlight_line(this)" onmouseout="clear_highlight_line(this)">');
                            html.push('<td>');
                            html.push(result.result[i].fullName);
                            html.push('</td>');

                            html.push('<td>');
                            html.push(result.result[i].parentFullName);
                            html.push('</td>');

                            html.push('<td>');
                            html.push(result.result[i].username);
                            html.push('</td>');

                            html.push('<td>');
                            html.push(htmlEncode(result.result[i].desc));
                            html.push('</td>');

                            html.push('<td class="nowrap">');
                            html.push("<a href='#' onclick='versionChangeToEdit(true,");
                            html.push(i);
                            html.push(")'>编辑</a>");
                            html.push('&nbsp;&nbsp;');
                            html.push("<a href='#' onclick='delversion(");
                            html.push(result.result[i].id);
                            html.push(")'>删除</a>");

                            html.push('</td>');
                            html.push('</tr>');
                        }

                        $('#versionsData').html(html.join(''));
                        showSplitPages();
                    } else {
                    }
                }
            });


}
function toPage(index) {
    pageIndex = index;
    loadVersions();
}
function saveVersion() {
    if (checkUnique('branchName') && checkUnique('addversionName')) {
        $.post('saveVersion.py', {id: $("#versionID").val(), branch: $("#branchName").val(),
                    subbranch: $("#subBranchName").val(), name: $("#addversionName").val(),
                    baseversion: $("#baseversionFullName").val(), description: $("#addversionDescription").val(),
                    sendMail:$("#addversionSendMail").prop("checked")},
                function (result, status) {
                    if (status == 'success') {
                        if (result.success) {
                            if (versionOnEdit) {
                                versionChangeToEdit(false, 0);
                                showMessage("修改成功");

                            }
                            else {
                                showMessage("添加成功");
                            }
                            clearText();
                            loadVersions();
                        } else {
                            showMessage(result.message);
                        }
                    }
                });
    }
}
function setFullName(focuse) {
    var branch = $('#branchName').val();
    var subbranch = $('#subBranchName').val();
    var version = $('#addversionName').val();
    if ((branch.length > 0 && version.length > 0)) {
        var fullname;
        if (subbranch.length > 0) {
            fullname = branch + '-' + subbranch + '_' + version;
        }
        else {
            fullname = branch + '_' + version;
        }
        $('#addversionFullName').val(fullname);
    }
    else if (focuse) {
        $('#addversionFullName').val('');
    }
}
function getAutoBaseVersionFullName() {
    if (versionOnEdit)
        return;
    var branch = $('#branchName').val();
    var subbranch = $('#subBranchName').val();
    if (branch.length > 0 && subbranch.length > 0) {
        $.post('getAutoBaseVersionName.py', {branch: $("#branchName").val(), subbranch: $("#subBranchName").val()},
                function (result, status) {
                    if (status == 'success') {
                        if (result.success) {
                            $('#baseversionFullName').val(result.result);
                        }
                    }
                });
    }
}
function splitFullName(reset) {
    var fullname = $('#addversionFullName').val();
    re = /([^-_]+_[^-_]+_[^-_]+)-([^-_]+)_(.*)/g;
    if (re.test(fullname)) {
        var branch = RegExp.$1.toString();
        var subbranch = RegExp.$2.toString();
        var version = RegExp.$3.toString();
        $('#branchName').val(branch);
        $('#subBranchName').val(subbranch);
        $('#addversionName').val(version);
    }
    else {
        re = /([^-_]+_[^-_]+_[^-_]+)_(.*)/g;
        if (re.test(fullname)) {
            var branch = RegExp.$1.toString();
            var version = RegExp.$2.toString();
            $('#branchName').val(branch);
            $('#addversionName').val(version);
            $('#subBranchName').val('');
        }
        if (reset) {
            setFullName(true);
        }
    }
}
function enterKeyLoadVersions(event) {
    if (event.which == 13) {
        loadVersions();
    }
}
function trimFullName(str) {
    var re1 = /^user_(.*)/g;
    var re2 = /^eng_(.*)/g;
    var re3 = /(.*)\.zip$/g;
    if (re1.test(str)) {
        str = RegExp.$1.toString();
    }
    if (re2.test(str)) {
        str = RegExp.$1.toString();
    }
    if (re3.test(str)) {
        str = RegExp.$1.toString();
    }
    return str;
}
$(function () {
    $('.mybutton').button();
    $('#versionNameKey').keydown(enterKeyLoadVersions);
    versionChangeToEdit(false, 0);
    loadVersions();


    $('#branchName').keyup(function () {
        setFullName(false);
    });
    $('#subBranchName').keyup(function () {
        setFullName(false);
    });
    $('#addversionName').keyup(function () {
        setFullName(false);
    });


    $('#addversionFullName').keyup(function () {
        var str = trimFullName($('#addversionFullName').val());
        if (str != $('#addversionFullName').val()) {
            $('#addversionFullName').val(str);
        }
        splitFullName(false);
    });
    $('#addversionFullName').blur(function () {
        var str = trimFullName($('#addversionFullName').val());
        if (str != $('#addversionFullName').val()) {
            $('#addversionFullName').val(str);
        }
        splitFullName(false);
        getAutoBaseVersionFullName();
    });
    $('#baseversionFullName').focus(getAutoBaseVersionFullName);
    var cache = {};
    $("#branchName").autocomplete({
        minLength: 1,
        source: function (request, response) {
            var term = request.term;

            if (term in cache) {
                response(cache[ term ]);
                return;
            }

            $.getJSON("searchBranchesName.py", request, function (data, status, xhr) {
                if (data.success) {

                    cache[ term ] = data.result;
                    response(data.result);
                }
            });
        }
    });

    $("#subBranchName").autocomplete({
        minLength: 0,
        source: function (request, response) {
            request.branch = $("#branchName").val();
            var key = request.branch + ',' + request.term;

            if (key in cache) {
                response(cache[ key ]);
                return;
            }

            $.getJSON("searchSubBranchesName.py", request, function (data, status, xhr) {
                if (data.success) {
                    cache[ key ] = data.result;
                    response(data.result);
                }
            });
        }
    });

    $("#baseversionFullName").autocomplete({
        minLength: 1,
        source: function (request, response) {
            var key = 'full,' + request.term;

            if (key in cache) {
                response(cache[ key ]);
                return;
            }

            $.getJSON("searchVersionFullName.py", request, function (data, status, xhr) {
                if (data.success) {
                    cache[ key ] = data.result;
                    response(data.result);
                }
            });
        }
    });
});
</script>
</head>
<body>
<h1 style="text-align: center" class="ui-dialog-titlebar">版本管理</h1>
<hr>

<input type="hidden" id="versionID">
<table border="0" style="margin: auto;">
    <tr>
        <td><label for="branchName">所属分支</label></td>
        <td><input type="text" style="width: 250px" id="branchName"></td>
        <td><label id="branchNameMsg" class="ui-state-error ui-helper-hidden"></label></td>
    </tr>
    <tr>
        <td><label for="subBranchName">所属子分支</label></td>
        <td><input type="text" style="width: 250px" id="subBranchName"></td>
        <td><label id="subBranchNameMsg" class="ui-state-error ui-helper-hidden"></label></td>
    </tr>
    <tr>
        <td><label for="addversionName">版本编号</label></td>
        <td><input type="text" style="width: 250px" id="addversionName"></td>
        <td><label id="addversionNameMsg" class="ui-state-error ui-helper-hidden"></label></td>
    </tr>
    <tr>
        <td><label style="color: blue" for="addversionFullName">版本全名</label></td>
        <td><input type="text" style="width: 250px" id="addversionFullName"></td>
        <td><label id="addversionFullNameMsg" class="ui-state-error ui-helper-hidden"></label></td>
    </tr>
    <tr>
        <td><label style="color: forestgreen" for="baseversionFullName">基于版本</label></td>
        <td><input type="text" style="width: 250px" id="baseversionFullName"></td>
        <td><label id="baseversionFullNameMsg" class="ui-state-error ui-helper-hidden"></label></td>
    </tr>
    <tr>
        <td><label for="addversionDescription">版本说明</label></td>
        <td><textarea id="addversionDescription" style="width: 250px;height: 100px"></textarea></td>
        <td>&nbsp;&nbsp;&nbsp;</td>
    </tr>
    <tr>
        <td><label for="addversionSendMail">发送邮件</label></td>
        <td><input type="checkbox" id="addversionSendMail" checked="checked"></td>
        <td>&nbsp;&nbsp;&nbsp;</td>
    </tr>
    <tr>
        <td colspan="3" style="text-align: right">
            <button class="mybutton" onclick="saveVersion();" id="editversionOK">修改</button>
            <button class="mybutton" onclick="versionChangeToEdit(false,0);" id="editversionCancel">取消</button>
            <button class="mybutton" onclick="saveVersion();" id="addversion">添加</button>
        </td>
    </tr>
</table>

<div id="list" style="text-align: center">
    <hr>
    <input type="text" id="versionNameKey" style="width: 60%;">
    <button class="mybutton" onclick="loadVersions();" style="font-size: 13px;">查找</button>
    <table border="1" style="width: 100%">
        <thead>
        <th>版本全名</th>
        <th>基于版本</th>
        <th>用户名</th>
        <th>版本说明</th>
        <th>操作</th>
        </thead>
        <tbody id="versionsData"></tbody>
    </table>
    <div class="digg" id="pages"></div>
</div>
</body>
</html>